package ebcs.scan.controller.sys;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import ebcs.database.bean.sys.PaTimerTask;
import ebcs.database.service.sys.ITimerManagerService;
import ebcs.scan.common.consts.TimerStatus;
import jplat.core.trans.JAppBaseService;
import jplat.core.trans.JAppContext;
import jplat.core.trans.JAppContextFactory;
import jplat.core.trans.JSpringContextUtil;
import jplat.error.exception.JSystemException;
import jplat.error.exception.JTransException;
import jplat.tools.coder.JsonCoder;
import jplat.tools.string.DateUtil;


/***
 * timer定时器
 * @author chenzhuo
 * @comment
 */
@RequestMapping("/ns0/timer/")
@Controller
@EnableScheduling
public class TimerManagerCtrl extends JAppBaseService
{
	private Logger logger = Logger.getLogger(TimerManagerCtrl.class);
	protected JAppContextFactory serviceFactory = new JAppContextFactory();

	@Autowired
	private ITimerManagerService timerService;

	@Autowired
    private JSpringContextUtil springContextsUtil;

	@RequestMapping("/doControllerTimerStart.do")
	public void getParameter( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);
		doRunTimer();
		context.buildAppResponse(null);
	}
	@RequestMapping("/doClearTaskCache.do")
	public void doClearTaskCache( HttpServletRequest request,HttpServletResponse response ) throws JTransException, JSystemException{
		JAppContext context = serviceFactory.buildAppContextNoUser(request, response);
		this.timerService.evictTimerListCache();
		context.buildAppResponse(null);
	}
	//每分钟启动一次
 	@Scheduled(cron = "*/1 * * * * ?")
 	public void doRunTimer() {
 		List<PaTimerTask> timerList = this.timerService.searchAllTimerList();
		for (PaTimerTask task : timerList) {
				 String beanName = task.getServiceName();
				 String methodName = task.getMethodName();
				 String taskTimer = task.getTaskTimer();
				 String timerformat = task.getTimerFormat();
				 String nowTime = DateUtil.getSystemTimeBySomeFormat(timerformat);
				 if(nowTime.equals(taskTimer)) {
					 
					 logger.info("timer run:"+nowTime+",run taskName:"+task.getTaskName()+",beanName:"+beanName+",methodName:"+methodName);
					 Method  mh = ReflectionUtils.findMethod(springContextsUtil.getBean(beanName).getClass(),methodName,new Class[]{} );
					 ReflectionUtils.invokeMethod(mh, springContextsUtil.getBean(beanName), new Object[] {});
					 task.setTaskOnoff(TimerStatus.TimerOff.getValue());//把定时器关停掉。
					 this.timerService.updateTimerTask(task);
					 
				 }
		}
 	}
}